package com.shanzmoo.admin.web.store;

import com.shanzmoo.admin.domain.dto.StoreDeliveryBillSendDTO;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.core.domain.dto.StoreDeliveryBillSaveDTO;
import com.shanzmoo.core.domain.dto.StoreDeliveryBillUpdateDTO;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.manager.store.StoreDeliveryBillManager;
import com.shanzmoo.db.store.entity.query.StoreDeliveryBillQuery;
import com.shanzmoo.db.store.entity.vo.StoreDeliveryBillVO;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 仓库发货单 控制层
 *
 * @author Gzhao 2021-03-11
 */
@Api(tags = "仓库发货单")
@RestController
@RequestMapping("/store/delivery-bill")
public class StoreDeliveryBillController {

    @Autowired
    private StoreDeliveryBillManager storeDeliveryBillManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreDeliveryBillVO>> pageList(@Valid @RequestBody StoreDeliveryBillQuery query) {
        PageRespBase<StoreDeliveryBillVO> pageList = storeDeliveryBillManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "手动出库")
    @PostMapping("/manualOut")
    public SHttpResponse<Boolean> manualOut(@Valid @RequestBody StoreDeliveryBillSaveDTO saveDto) {
        SResponse<Boolean> re = storeDeliveryBillManager.manualOut(saveDto, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "取消发货单")
    @GetMapping("/cancel")
    public SHttpResponse<Boolean> cancel(
            @NotEmpty(message = "发货单id不允许为空") @ApiParam(value = "发货单id") @RequestParam(value = "id") Integer id,
            @NotEmpty(message = "取消原因不允许为空") @ApiParam(value = "取消原因")  @RequestParam(value = "cancelReason") String cancelReason
    ) {
        SResponse<Boolean> re = storeDeliveryBillManager.cancel(id, cancelReason);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "确认发货单状态")
    @GetMapping("/confirm")
    public SHttpResponse<Boolean> confirm(
            @NotEmpty(message = "发货单id不允许为空") @ApiParam(value = "发货单id") @RequestParam(value = "id") Integer id
    ) {
        SResponse<Boolean> re = storeDeliveryBillManager.confirm(id, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody StoreDeliveryBillUpdateDTO updateDto) {
        SResponse<Boolean> re = storeDeliveryBillManager.update(updateDto, CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "发货")
    @PostMapping("/send")
    public SHttpResponse<Boolean> send(@Valid @RequestBody StoreDeliveryBillSendDTO sendDTO) {
        SResponse<Boolean> re = storeDeliveryBillManager.send(sendDTO.convertEntity(), CurrentUserCache.getUserId());
        return SHttpResponse.transform(re);
    }
 }
